package com.android.kmlmrg.utils;

import java.security.AccessController;
import java.security.Provider;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;



public class MailHelper  extends javax.mail.Authenticator implements Const {

	private static MailHelper mailHelper = null ;
	
	private String host;
	
	private String port;
	
	private String from; 
	
	private String sport;
	
	private String body;
	
	private String subject;
	
	private boolean debuggable;
	
	private boolean auth;
	
	private Properties props;
	
	private String username ;
	
	private String password ;
	
	private String[] recipients;
	
	private MimeMultipart multipart;
	
	static {   
        Security.addProvider(new JSSEProvider());   
    }  
	
	public MailHelper (){

		setHost("gmail-smtp-msa.l.google.com"); 
	    setPort("465");  
	    setSport("465"); 
	    setUsername(STRING_EMPTY);
	    setPassword(STRING_EMPTY);
	    setFrom("KMLGMR.test@gmail.com");
	    setDebuggable(false); 
	    setAuth(true); 

	    
	    initProperties();

	}
	
	public static MailHelper getInstance(){
		
		if(mailHelper == null){
			mailHelper = new MailHelper();
		}
		
		return mailHelper;
	}
	public void send(String pSubject, String  pBody, String[] pRecipients) throws AddressException, MessagingException,Exception {
		setBody(pBody);
		setSubject(pSubject);
		this.setRecipients(pRecipients);
	    if( (recipients != null) && (recipients.length>0) && !from.equals("") && !subject.equals("") && !body.equals("")) { 
	        Session session = Session.getDefaultInstance(props,this); 
	        
	        MimeMessage msg = new MimeMessage(session); 
	        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));  
	        msg.setSender(new InternetAddress(from));
	        msg.setSubject(pSubject); 
	        msg.setDataHandler(handler);
	        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[0]));   
	        Transport.send(msg); 

	      } 
	        
		
	}
	
	private void initProperties() { 
	     props = new Properties(); 
	    props.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_PROTOCOL);
	    props.setProperty(MAIL_SMTP_HOST, host); 
	 
	    if(debuggable) { 
	      props.put(MAIL_DEBUG, "true"); 
	    } 
	 
	    if(auth) { 
	      props.put(MAIL_SMTP_AUTH, "true"); 
	    } 
	 
	    props.put(MAIL_SMTP_PORT , port); 
	    props.put(MAIL_SMTP_SOCKETFACTORY_PORT, sport); 
	    props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); 
	    props.put(MAIL_SMTP_SOCKETFACTORY_FALLBACK, "false"); 
	    props.setProperty(MAIL_SMTP_QUITWAIT, "false");
	}
	
	@Override
	public PasswordAuthentication getPasswordAuthentication() { 
	    return new PasswordAuthentication(username, password); 
	  }
	
	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public boolean isDebuggable() {
		return debuggable;
	}

	public void setDebuggable(boolean debuggable) {
		this.debuggable = debuggable;
	}

	public boolean isAuth() {
		return auth;
	}

	public void setAuth(boolean auth) {
		this.auth = auth;
	}

	public MimeMultipart getMultipart() {
		return multipart;
	}

	public void setMultipart(MimeMultipart multipart) {
		this.multipart = multipart;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String[] getRecipients() {
		return recipients;
	}

	public void setRecipients(String[] recipients) {
		this.recipients = recipients;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

}

class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS","org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}


